<?php 
namespace Api\Controller;
class StudentController extends CommonController{
	
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		//$this->m = D('YgclTerm');
	}
	
	
	/**
	 * 更新学生表关联用户的uid
	 * @param unknown $id
	 * @param unknown $tel
	 */
	public function uid_relation_by_tel($id,$tel){
		$info = M('Users')->where(array('mobile'=>$tel))->find();
		if($info){
			$uid = $info['uid'];
			M('LessonStudentRelation')->where(array('id'=>$id))->setField('uid',$uid);
		}
	}
	
	/**
	 * 会员首次验证登录更新学生表关联用户的uid
	 * @param unknown $uid
	 * @param unknown $tel
	 */
	public function users_relation_by_tel($uid,$tel){
		M('LessonStudentRelation')->where(array('tel'=>$tel))->setField('uid',$uid);
	}
	
	/**
	 * 获取老生数据
	 * @param unknown $type
	 * @param unknown $id
	 */
	public function get_old_student($type,$id){
		if($type == 'oldstu'){
			$m = D('YgclOldstudent');
		}else{
			$m = D('LessonStudentRelation');
		}
		return $m->where(array('id'=>$id))->relation(true)->find();
		
	}
		
	public function get_old_student_lid($student,$tel){
		$xuenian = R('Api/Termtime/get_term_time_now_and_past');
		$lid = array();
		foreach ($xuenian as $xn){
			$map = array(
					'student' => $student,
					'tel' => $tel,
					'xuenian' => array('like','%X'.$xn['id'].'N%')
			);
			$info = M('LessonStudentRelation')->where($map)->getField('lid',true);
			foreach ($info as $if){
				array_push($lid,$if);
			}
		}
		$info = M('YgclOldstudent')->where(array('student'=>$student,'tel'=>$tel))->getField('lid',true);
		foreach ($info as $if){
			array_push($lid,$if);
		}
		return array_unique($lid);
	}
	
	/**
	 * 根据所报学年和班级获取学生信息
	 * @param unknown $xnid
	 * @param unknown $lid
	 * @return Ambigous <\Think\mixed, boolean, string, NULL, mixed, unknown, multitype:, object>
	 */
	public function get_old_student_by_tid_lid($xnid,$lid){
		return M('LessonStudentRelation')->where(array('xuenian'=>array('like','%X'.$xnid.'N%'),'lid'=>$lid))->select();
	}
	
	/**
	 * 获取学生报名的老新生区别
	 * @param unknown $pre
	 * @param unknown $next
	 */
	public function get_new_or_old_student($old_students,$next_students){
		foreach ($next_students as $ns){
			if($this->get_if_or($ns, $old_students)){
				$sdata['lao'][] = $ns;
			}else{
				$sdata['new'][] = $ns;
			}
		}
		return $sdata;
	}
	
	public function get_if_or($new_student,$old_students){
		$b = false;
		foreach ($old_students as $os){
			if($new_student['student'] == $os['student'] && $new_student['tel'] == $os['tel']){
				$b= true;
				break;
			}
		}
		return $b;
	}
	
	//判断老生是否在老的学年里面
	public function _check_student_in_pre_xuenian($old_students,$student,$tel){
		$bool = false;
		foreach ($old_students as $os){
			if($student == $os['student'] && $tel == $os['tel']){
				$b = true;
				break;
			}
		}
		return $b;
	}
	
	/**
	 * 签到保存
	 */
	public function savesign(){
		if(IS_POST){
			$xuenian = R('Api/Termtime/get_term_time_now');
			$data['xnid'] = $xuenian['id'];
			$data['lid'] = $_POST['lid'];
			
			$users = $_POST['users'];
			if(!$users){
				$this->error('请先选择学生是否已到或请假');
				exit();
			}
			foreach ($users as $key=>$us){
				if($us == 1) $dao[] = $key;
				if($us == 2) $weidao[] = $key;
				if($us == 3) $qingjia[] = $key;
			}
			if($dao){
				$data['dao'] = implode(',', $dao);
				$data['daoname'] = $this->getstuname($dao);
			}else{
				$data['dao'] = $data['daoname']="";
			}
			if($qingjia){
				$data['qingjia'] = implode(',', $qingjia);
				$data['qingjianame'] = $this->getstuname($qingjia);
			}else{
				$data['qingjia'] = $data['qingjianame']="";
			}
			
			//$data_weidao = $this->getweidao_info($data['dao'],$data['qingjia'],$data['lid']);
			//if($data_weidao){
			//	$data['weidao'] = implode(',', $data_weidao);
			//	$data['weidaoname'] = $this->getstuname($data_weidao);
			//}else{
			//	$data['weidao'] = $data['weidaoname'] = '';
			//}
			
			
			$m = M('YgclSign');
			$data['update_time'] = Date('Y-m-d H:i:s');
			if($_POST['id']){
				$data['id'] = $_POST['id'];
				if($m->save($data)){
					R('Api/Sign/save_sign_list',array('sid'=>$data['id'],'uid'=>$_POST['uid'],'dao'=>$data['dao'],'qingjia'=>$data['qingjia']));
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}
			}else{
				
				$data['title'] = $_POST['title'];
				$where = array(
					'lid' => $data['lid'],
					'title'=> $data['title'],
					'xnid' => $data['xnid'],
				);
				if($m->where($where)->find()){
					$this->error('当前时间已有签到记录，请不要重复添加');
				}else{
					$data['create_time'] = $data['update_time'];
					$id = $m->add($data);
					if($id){
						R('Api/Sign/save_sign_list',array('sid'=>$id,'uid'=>$_POST['uid'],'dao'=>$data['dao'],'qingjia'=>$data['qingjia']));
						$this->success('添加成功');
					}else{
						$this->error('添加失败');
					}
				}
			}
		}
	}
	
	public function getstuname($lsrids){
		$students = M("LessonStudentRelation")->where(array('id'=>array('in',$lsrids)))->getField('student',true);
		return implode(',',$students);
	}
	
	public function getweidao($dao,$qingjia,$lid){
		$weidao = $this->getweidao_info($dao,$qingjia,$lid);
		if($weidao){
			return  $this->getstuname($weidao);
		}else{
			return '';
		}
	}
	
	public function getweidao_info($dao,$qingjia,$lid){
		$stu = $dao.','.$qingjia;
		$stu_arr = array_filter(explode(',', $stu));
	
		//班级总的学生
		$students = R('Api/Lessonapi/get_students_by_now_xnid',array($lid));
		foreach ($students as $stu){
			$ss[] = $stu['id'];
		}
	
		$weidao_arr = array_diff($ss,$stu_arr);
		return  $weidao_arr;
	}

}